using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Win32;
using System.Diagnostics;

namespace Macaw.Moss2007.SearchEnhancements
{
    public class ChangeGathererUserAgentFeatureReceiver : SPFeatureReceiver
    {

        private string GathererUserAgent
        {
            get
            {
                string useragent = null;
                SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Office Server\\12.0\\Search\\Global\\Gathering Manager");
                    if (key != null)
                    {
                        useragent = (string)key.GetValue("UserAgent");
                        key.Close();
                    }
                });
                return useragent;
            }
            set
            {
                SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Office Server\\12.0\\Search\\Global\\Gathering Manager", true);
                    if (key != null)
                    {
                        key.SetValue("UserAgent", value);
                        key.Close();
                    }
                });
            }
        }

        private const string ROBOTUSERAGENT = "MS Search 5.0 Robot";
        private const string CRAWLERUSERAGENT = ROBOTUSERAGENT + " Crawler";

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                string currentuseragent = GathererUserAgent;
                if (currentuseragent != null)
                {
                    if (currentuseragent.Contains(CRAWLERUSERAGENT))
                    {
                        EventLog.WriteEntry(this.GetType().FullName, string.Format("The current UserAgent string ({0}) is already changed to be recognized by ASP.NET as Crawler/Robot.", currentuseragent), EventLogEntryType.Information);
                    }
                    else if (currentuseragent.Contains( ROBOTUSERAGENT ))
                    {
                        string newuseragent = currentuseragent.Replace(ROBOTUSERAGENT, CRAWLERUSERAGENT);
                        EventLog.WriteEntry(this.GetType().FullName, string.Format("The current UserAgent string ({0}) is being changed to ({1})", currentuseragent, newuseragent), EventLogEntryType.Information);
                        GathererUserAgent = newuseragent;
                    }
                    else
                    {
                        throw new Exception(string.Format("The current UserAgent string ({0}) is not recognized. This feature expect a substring of \"{1}\".", currentuseragent, ROBOTUSERAGENT) );
                    }
                }
            });
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                string currentuseragent = GathererUserAgent;
                if (currentuseragent != null)
                {
                    if (currentuseragent.Contains(CRAWLERUSERAGENT))
                    {
                        string newuseragent = currentuseragent.Replace(CRAWLERUSERAGENT , ROBOTUSERAGENT );
                        EventLog.WriteEntry(this.GetType().FullName, string.Format("The current UserAgent string ({0}) is being changed to ({1})", currentuseragent, newuseragent), EventLogEntryType.Information);
                        GathererUserAgent = newuseragent;
                    }
                }
            });
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
        }
    }
}
